package com.ze.java.test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author:yangzy
 * @Date:2021/6/16 14:31
 */
public class GeoServer {

    private static final int width = 200;
    private static final int heigh = 100;

    public static void main(String[] args) {
        double lonMin = 102.2602;
        double lonMax = 127.247;
        double latMin = 15.472;
        double latMax = 25.837;
        String url = "http://192.168.3.165:9080/geoserver/fzty/wms?service=WMS&format=image%2Fpng&transparent=true&tiled=true&version=1.1.1&request=GetMap&styles=&layers=fzty:srtm-250&" +
                "bbox=" + lonMin + "," + latMin + "," + lonMax + "," + latMax + "&width=1300&height=1000&srs=EPSG%3A4326";
        url = "http://192.168.3.165:9080/geoserver/elevation/wms?service=WMS&format=image/png&transparent=true&tiled=true&version=1.1.1&request=GetMap&styles=&layers=elevation:srtm-250&bbox=122.569715,33.076279,130.14515,36.071649&width=2000&height=1000&srs=EPSG:4326";
        try {
            BufferedImage bufferedImage = null;
            try {
                bufferedImage = ImageIO.read(getImage(url));
            } catch (IOException e) {
                e.printStackTrace();
            }
            int w = bufferedImage.getWidth();
            int h = bufferedImage.getHeight();
            int[][] gray = new int[w][h];
            for (int i = 0; i < w; i++) {
                for (int j = 0; j < h; j++) {
                    int rgb = bufferedImage.getRGB(i, j);
                    gray[i][j] = getImageRgb(rgb);
                }
            }

            int[][] grid = new int[width][heigh];
            int grayI = 0, grayJ = 0, xSize = w / width, ySize = h / heigh;
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < heigh; j++) {
                    grid[i][j] = getGray(gray, grayI, grayJ, xSize, ySize);
                    grayJ += ySize;
                }
                grayI += xSize;
                grayJ = 0;
            }

            for (int i = 0; i < heigh; i++) {
                for (int j = 0; j < width; j++) {
                    System.out.print(grid[j][i] + " ");
                }
                System.out.println();
            }
            int value = grid[134][97];
            System.out.println(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int getImageRgb(int i) {
        Color color = new Color(i);
        // argb分别代表透明,红,绿,蓝 分别占16进制2位
        int r = color.getRed();
        int g = color.getGreen();
        int b = color.getBlue();
        int result = ((r + g + b) / 3);
        return result;
    }

    public static int getGray(int gray[][], int x, int y, int xSize, int ySize) {
        int rs = 0;
        for (int i = x; i < xSize + x; i++) {
            for (int j = y; j < ySize + y; j++) {
                rs += gray[i][j];
            }
        }
        return (rs / (xSize * ySize)) > 200 ? 0 : 1;
    }

    public static InputStream getImage(String strUrl) {
        try {
            URL url = new URL(strUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            InputStream stream = connection.getInputStream();
            return stream;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//    public void geo() {
//        String url = "http://192.168.3.165:9080/geoserver";
//        try {
//            URL u = new URL(url);
//            GeoServerRESTReader reader = new GeoServerRESTReader(u, "admin", "geoserver");
//
//            RESTResource resource = reader.getResource(RESTLayer.build(""));
//            RESTLayerGroup layerGroup = reader.getLayerGroup("srtm-250");
//            RESTLayerList layers = layerGroup.getLayerList();
//            layers.forEach(nameLinkElem -> {
//                System.out.println(nameLinkElem.getName());
//            });
//            System.out.println(layerGroup.getRootLayer());
//
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
//    }
}
